/** @file         cdev_manager_demo_drv.c
 *  @brief        cdev_manager 使用范例
 *  @details      详细说明
 *  @author       Schips
 *  @date         2020-10-25 22:02:20
 *  @version      v1.0
 *  @copyright    Copyright By Schips, All Rights Reserved
 *
 **********************************************************
 *
 *  @attention    NOTE
#*                SDK: 
#*                ENV: 
 *  @par 修改日志:
 *  <table>
 *  <tr><th>Date       <th>Version   <th>Author    <th>Description
 *  <tr><td>2020-10-25 <td>1.0       <td>Schips    <td>创建初始版本
 *  </table>
 *
 **********************************************************
 */

#include <linux/module.h>  
#include <linux/kernel.h>
#include <linux/init.h>  
#include <linux/sched.h>
#include <linux/ioctl.h>
#include <linux/sched/signal.h>
#include <linux/ide.h>  
#include <linux/types.h>  
#include <linux/errno.h>
#include <linux/cdev.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#include <linux/semaphore.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/irq.h>
#include <linux/wait.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/timer.h>
#define CONFIG_FOR_KERNEL
#include "cdev_manager.h"

#define PRINT_LV        "<3>"
#define AVALON_REG_SIZE 16
#ifdef MODULES_ALL_FOR_ONE
    // 单独驱动模式(小驱动作为接口，而不是模块)
    #define MODULE_INIT_API int
    #define MODULE_EXIT_API void
#else
    #define MODULE_INIT_API static int  __init
    #define MODULE_EXIT_API static void __exit
#endif

/* 设备节点名称 */  
#define DRIVER_NAME       "cdev_manager_drv_demo"
/* 设备号个数 */  
#define DEVID_COUNT       1
/* 驱动个数 */  
#define DRIVE_COUNT       1
/* 次设备号 */
#define MINOR_U           0

/* 前导声明函数*/
static int cdev_manager_demo_drv_open(struct inode *inode_p, struct file *file_p);
static ssize_t cdev_manager_demo_drv_read(struct file *file_p, char __user *buf, size_t len, loff_t *loff_t_p);  
unsigned int cdev_manager_demo_drv_poll(struct file *filp, struct poll_table_struct *wait);
static int cdev_manager_demo_drv_release(struct inode *inode_p, struct file *file_p);

static struct file_operations demo_fops = {  
    .owner          = THIS_MODULE,  
    .open           = cdev_manager_demo_drv_open,  
    .read           = cdev_manager_demo_drv_read,   
    .release        = cdev_manager_demo_drv_release,   
};  

/** 系统调用实现 **/
static int cdev_manager_demo_drv_open(struct inode *inode_p, struct file *file_p)  
{  
    PRINT("cdev_manager_demo_drv_open\n");  
    return 0;  
}  

static ssize_t cdev_manager_demo_drv_read(struct file *file_p, char __user *buf, size_t len, loff_t *loff_t_p)  
{  
    PRINT("cdev_manager_demo_drv_read\n");
    return 0;  
}  

static int cdev_manager_demo_drv_release(struct inode *inode_p, struct file *file_p)  
{  
    PRINT("cdev_manager_demo_drv_release\n");
    return 0;  
}  

MODULE_INIT_API cdev_manager_demo_drv_init(void)  
{
    int ret;
    PRINT(PRINT_LV "cdev_manager_demo_drv_init\n");
    /* 创建 cdev 设备节点 */
    ret = cm_add_cdev(DRIVER_NAME,  // 驱动名称
                1,                  // 驱动数量
                &demo_fops);        // 对应的文件操作
    if(ret) return ret;
    cm_dump();
	return 0;
}

/* 卸载模块 */  
MODULE_EXIT_API cdev_manager_demo_drv_exit(void)  
{  
    PRINT(PRINT_LV "cdev_manager_demo_drv_exit\n");
    /* 移除 cdev 设备节点 */
    cm_remove_cdev(DRIVER_NAME);
}  

#ifndef MODULES_ALL_FOR_ONE
module_init(cdev_manager_demo_drv_init);  
module_exit(cdev_manager_demo_drv_exit);  

/* 驱动描述信息 */  
MODULE_AUTHOR("Demo for Schips");
MODULE_ALIAS("demo");  
MODULE_DESCRIPTION("Cdev Manager Demo");
MODULE_LICENSE("GPL");
MODULE_VERSION("v1.0");
#endif

